using UnityEngine;

namespace Neatly.UI
{
    [RequireComponent(typeof(EffectImage))]
    public class EffectImageQueue : NUIBehaviour
    {
        [SerializeField] public Sprite[] m_Sprites;
        [SerializeField] private bool m_IsLoop;
        [SerializeField] private float m_AllTime = 3;
        [SerializeField] private float m_IntervalTime = 0;
        private int m_Index;
        private float m_DelayTime;
        private EffectImage m_Image;
        private EffectImage Image
        {
            get
            {
                if (m_Image == null)
                {
                    m_Image = gameObject.GetComponent<EffectImage>();
                }
                return m_Image;
            }
        }

        protected override void OnEnable()
        {
            m_Index = 0;
            m_DelayTime = 0;
            if (m_AllTime > 0)
            {
                m_IntervalTime = m_AllTime / m_Sprites.Length;
            }
            if (m_IntervalTime == 0)
            {
                m_IntervalTime = float.MaxValue;
            }
            Image.sprite = m_Sprites[m_Index];
        }

        private void Update()
        {
            m_DelayTime += Time.deltaTime;
            if (m_DelayTime < m_IntervalTime) return;
            m_DelayTime -= m_IntervalTime;
            m_Index++;
            if (m_Index >= m_Sprites.Length)
            {
                if (!m_IsLoop)
                {
                    gameObject.SetActive(false);
                    return;
                }
                m_Index = 0;
            }
            Image.sprite = m_Sprites[m_Index];
        }
    }
}